﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Warranty.Model.Interface;
using Warranty.Model.Model;
using Common.Util;

namespace Warranty.Provider
{
    public class DealerProvider : GenericProvider, IDealerProvider
    {
        public List<Dealer> SearchDealer(out int totalRecords, string textsearch, int pageindex = 0, int pagesize = 0)
        {
            var query = repository.GetQuery<Dealer>();
            if (!textsearch.IsNullOrWhiteSpace())
            {
                query = query.Where(x => x.Model.ToUpper().Contains(textsearch.ToUpper()) ||
                                         x.SerialNo.ToUpper().Contains(textsearch.ToUpper()) ||
                                         x.EuName.ToUpper().Contains(textsearch.ToUpper()) ||
                                         x.ShopCode.ToUpper().Contains(textsearch.ToUpper()));
            }
            query = query.OrderBy(x => x.VrfFlag).ThenBy(x => x.CfmFlag).ThenByDescending(x => x.SelloutDate);
            totalRecords = query.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                query = query.Skip(pageindex * pagesize).Take(pagesize);
            }
            return query.ToList();
        }

        public List<Dealer> SearchDealer(out int totalRecord, string textsearch, string[] shopcodes, bool? verifyFlag, bool? confirmFlag, DateTime? fromdate, DateTime? todate, int pageindex = 0, int pagesize = 0)
        {
            var query = repository.GetQuery<Dealer>();

            if (!textsearch.IsNullOrWhiteSpace())
            {
                query = query.Where(x => x.Model.ToUpper().Contains(textsearch.ToUpper()) ||
                                         x.SerialNo.ToUpper().Contains(textsearch.ToUpper()) ||
                                         x.EuName.ToUpper().Contains(textsearch.ToUpper()) ||
                                         x.ShopCode.ToUpper().Contains(textsearch.ToUpper()));
            } 

            if (fromdate.HasValue)
            {
                query = query.Where(x => x.SelloutDate >= fromdate);
            }

            if (todate.HasValue)
            {
                query = query.Where(x => x.SelloutDate <= todate);
            }

            if (confirmFlag.HasValue)
            {
                string CfmFlag = confirmFlag.Value.ToBooleanString();
                query = query.Where(x => x.CfmFlag.ToUpper() == CfmFlag);
            }

            if (verifyFlag.HasValue)
            {
                string VrfFlag = verifyFlag.Value.ToBooleanString();
                query = query.Where(x => x.VrfFlag.ToUpper() == VrfFlag);
            }

            if (shopcodes != null)
            {
                query = query.Where(x => shopcodes.Contains(x.ShopCode));
            }

            query = query.OrderBy(x => x.VrfFlag).ThenBy(x => x.CfmFlag).ThenByDescending(x => x.SelloutDate);
            totalRecord = query.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                query = query.Skip(pageindex * pagesize).Take(pagesize);
            }
            return query.ToList();
        }

        public Dealer RetrieveDealer(string shopCode, string ediNo, DateTime? selloutDate)
        {
            var shop = (shopCode ?? string.Empty).ToUpper();
            var edi = (ediNo ?? string.Empty).ToUpper();
            return repository.FindOne<Dealer>(x => x.ShopCode.ToUpper() == shop && x.EdiNo.ToUpper() == edi && x.SelloutDate == selloutDate);
            repository.UnitOfWork.SaveChanges();
        }


        public bool Update(Dealer dealer)
        {
            try
            {
                var check = RetrieveDealer(dealer.ShopCode, dealer.EdiNo, dealer.SelloutDate);
                if (check == null) return false;

                check.BoxNo = dealer.BoxNo;
                check.CfmComment = dealer.CfmComment;
                check.CfmDate = dealer.CfmDate;
                check.CfmFlag = dealer.CfmFlag;
                check.CfmUser = dealer.CfmUser;
                check.EdiDate = dealer.EdiDate;
                check.EdiFile = dealer.EdiFile;
                check.EuAddress = dealer.EuAddress;
                check.EuCell = dealer.EuCell;
                check.EuName = dealer.EuName;
                check.Model = dealer.Model;
                check.ModelDesc = dealer.ModelDesc;
                check.SellinDate = dealer.SellinDate;
                check.SerialNo = dealer.SerialNo;
                check.Suffix = dealer.Suffix;
                check.UpdateDate = dealer.UpdateDate;
                check.UploadUser = dealer.UploadUser;
                check.VrfFlag = dealer.VrfFlag;
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
